package com.zut.hospital.service.pharmacy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zut.hospital.controller.pharmacy.PurchaseController;
import com.zut.hospital.dao.diagnose.Mdc_Rec_RelationMapper;
import com.zut.hospital.dao.diagnose.PatientMapper;
import com.zut.hospital.dao.diagnose.RecordMapper;
import com.zut.hospital.dao.pharmacy.InventoryMapper;
import com.zut.hospital.dao.pharmacy.MedicineMapper;
import com.zut.hospital.dao.pharmacy.Sale_RecordMapper;
import com.zut.hospital.entity.diagnose.*;
import com.zut.hospital.entity.pharmacy.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-06-10 19:12
 **/
@Service
public class MedPatServiceImpl implements MedPatService {

    private static final Logger log = LoggerFactory.getLogger(MedPatServiceImpl.class);
    @Autowired
    PatientMapper patientMapper;

    @Autowired
    Mdc_Rec_RelationMapper mdc_rec_relationMapper;
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    Sale_RecordMapper sale_recordMapper;
    @Autowired
    InventoryMapper inventoryMapper;

    @Override
    public String layuiJsonParse(List<Map<String, String>> list, int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }

    @Override
    public Map selectAll(Patient patient){
        Map<String,String>map;
        map=new HashMap<>();
        map.put("pat_id",patient.getId().toString());
        map.put("pat_name",patient.getPatName());
        map.put("pat_sex",patient.getPatSex().toString());
        map.put("status",patient.getPatStatus().toString());
        map.put("contract",patient.getPatContact());
       return map;
    }
    public Map selectPat(Patient patient){
        double a[] = new double[10];
        double total=0;
        JSONObject map=new JSONObject();
        map.put("pat_id",patient.getId().toString());
        map.put("pat_name",patient.getPatName());
        map.put("pat_sex",patient.getPatSex().toString());
        map.put("status",patient.getPatStatus().toString());
        map.put("contract",patient.getPatContact());
        RecordExample recordExample=new RecordExample();
        recordExample.createCriteria().andRecPatIdEqualTo(patient.getId());
        Record record=recordMapper.selectByExample(recordExample).get(0);
        Mdc_Rec_RelationExample mdc_rec_relationExample=new Mdc_Rec_RelationExample();
        mdc_rec_relationExample.createCriteria().andRelatRecIdEqualTo(record.getId());
        List<Mdc_Rec_Relation>mdc_rec_relationList=mdc_rec_relationMapper.selectByExample(mdc_rec_relationExample);
        System.out.println("size"+mdc_rec_relationList.size());
        List<Map<String,String>>resaultmap=new ArrayList<>();
        int i=0;
        for (Mdc_Rec_Relation mdc_rec_relation:mdc_rec_relationList){

            Map<String,String>map1=new HashMap<>();
            MedicineExample medicineExample=new MedicineExample();
            medicineExample.createCriteria().andIdEqualTo(mdc_rec_relation.getRelatMdcId());
            Medicine  medicine=medicineMapper.selectByExample(medicineExample).get(0);
            map1.put("med_name",medicine.getMdcName());
            map1.put("med_price",medicine.getMdcRemark());
            map1.put("med_count",mdc_rec_relation.getRelatMdcCount().toString());
            resaultmap.add(map1);
            a[i]=Double.parseDouble(medicine.getMdcRemark())*mdc_rec_relation.getRelatMdcCount();
            i++;
        }

        for(int j=0;j<a.length;j++){
            total=total+a[j];
            System.out.println(total);
        }
        map.put("medicine",resaultmap);
        map.put("total",total);
        return map;
    }
    @Override
    public String getAll(){
        Date nowTime = new Date();
        System.out.println(nowTime);
        String number=new SimpleDateFormat("yyyy-MM-dd").format(nowTime);
        List<Patient>patientList=patientMapper.selectByTime(number);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        for (Patient patient:patientList){
            resaultmap.add(selectAll(patient));
        }
        return layuiJsonParse(resaultmap,resaultmap.size());
    }
    //提示库存
    @Override
    public String medInv(){
        int flag=0;
        InventoryExample inventoryExample=new InventoryExample();
        List<Inventory>inventoryList=inventoryMapper.selectByExample(inventoryExample);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        Map<String,String>map1=new HashMap<>();
        for (Inventory inventory:inventoryList){
            if (inventory.getIvtrCount()<inventory.getIvtrLimit()){
                MedicineExample medicineExample=new MedicineExample();
                medicineExample.createCriteria().andIdEqualTo(inventory.getIvtrMdcId());
                Medicine medicine=medicineMapper.selectByExample(medicineExample).get(0);
                map1.put("med",medicine.getMdcName());
                map1.put("count",inventory.getIvtrCount().toString());
                map1.put("id",inventory.getId().toString());

                    flag=1;
            }
        }
        resaultmap.add(map1);
        System.out.println(resaultmap.size());
        if(flag==0) {
            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 0);
            layuiMap.put("msg", "failed");
            layuiMap.put("count", 1);
            layuiMap.put("data", resaultmap);
            return JSON.toJSONString(layuiMap);

        }else{
            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 1);
            layuiMap.put("msg", "success");
            layuiMap.put("count", 1);
            layuiMap.put("data", resaultmap);
            return JSON.toJSONString(layuiMap);
        }

    }

    //忽略
    @Override
    public String setInv(int id){

        InventoryExample inventoryExample=new InventoryExample();
        inventoryExample.createCriteria().andIdEqualTo(id);
        Inventory inventory=inventoryMapper.selectByExample(inventoryExample).get(0);

        inventory.setIvtrLimit(0);
        inventoryMapper.updateByExample(inventory,inventoryExample);
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 1);
        layuiMap.put("data", "");
        return JSON.toJSONString(layuiMap);
    }
//主页显示拿药人数
    @Override
    public String checkStatue(){
        double money=0;
        Date date=new Date();
//        PatientExample patientExample=new PatientExample();
//        patientExample.createCriteria().andPatCreateTimeEqualTo(date);
        String number=new SimpleDateFormat("yyyy-MM-dd").format(date);
        List<Sale_Record>sale_recordList=sale_recordMapper.selectByTime(number);

        for (Sale_Record sale_record:sale_recordList){

                if (sale_record.getSaleRecType()!=2)
            money=money+sale_record.getSaleRecTotal();
        }
        List<Patient>patientList=patientMapper.selectByTime(number);
        List<Map<String, String>> resaultmap = new ArrayList<>();
        int i=0,j=0;
        for (Patient patient:patientList) {
            System.out.println("id"+patient.getId());
            if(patient.getPatStatus()==2){
                i++;
            }
            if (patient.getPatStatus()==3){
                j++;
            }

        }
        Map<String,String>map;
        map=new HashMap<>();
        map.put("s2",String.valueOf(i));
        map.put("s3",String.valueOf(j));
        map.put("money",String.valueOf(money));
        resaultmap.add(map);


        return layuiJsonParse(resaultmap,resaultmap.size());
    }
    //所有没拿药的人
    @Override
    public String getPatByName(String name){
        PatientExample patientExample=new PatientExample();
        patientExample.createCriteria().andPatNameEqualTo(name);
        List<Patient>patientList=patientMapper.selectByExample(patientExample);
        List<Patient>patientList1=new ArrayList<>();
        for (Patient patient1:patientList){
            if (patient1.getPatStatus()==2){
                patientList1.add(patient1);
            }
        }
        List<Map<String,String>>resaultmap=new ArrayList<>();
        for (Patient patient:patientList1){
            resaultmap.add(selectPat(patient));
        }
        return layuiJsonParse(resaultmap,resaultmap.size());
    }


}
